package com.gears_software.bbnetwork.activitys;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.adapters.TiXianAdapter;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.DrawHistoryModel;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.utils.SwipeRefreshLoadListView.view.XListView;
import com.gears_software.common.utils.SwipeRefreshLoadListView.view.XListView.IXListViewListener;
/**
 * 提现记录
 * @author DY-PC
 *
 */
@EActivity(R.layout.activity_tixianjilu)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class TiXianListActivity extends Activity {
	
	@ViewById
	XListView listview_tixian;
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	TiXianAdapter adapter;
	
	@Bean
	ToastUI toast;
	
	@ViewById
	TextView text;//没有
	
	Integer pageNum = 1;
	
	List<DrawHistoryModel> list = new ArrayList<DrawHistoryModel>();

	@AfterViews
	void onCreate() {
		listview_tixian.setPullRefreshEnable(false);
		listview_tixian.setPullLoadEnable(true);
		listview_tixian.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
			}
			@Override
			public void onLoadMore() {
				getLoad();
			}
		});
		getData();
	}

	
	@Background
	void getLoad(){
		ResultModel<List<DrawHistoryModel>> res = finder.getTixianList(sp.userId().get(),pageNum.toString());
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			addLoad(res.getResObj());
		}else{
			showTxt("数据读取错误！"+res.getMessage());
		}
	}
	
	@UiThread
	void addLoad(List<DrawHistoryModel> data){
		
		if(data!=null&&data.size()>0){
			list.addAll(data);
			adapter.notifyDataSetChanged();
			listview_tixian.stopLoadMore();
			pageNum++;
		}else{
			listview_tixian.stopLoadMore();
			listview_tixian.setPullLoadEnable(false);
			showTxt("没有更多数据了！");
		}
	}
	
	@Background
	void getData(){
		ResultModel<List<DrawHistoryModel>> res = finder.getTixianList(sp.userId().get(),pageNum.toString());
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			initData(res.getResObj());
		}else{
			showTxt("数据读取错误！"+res.getMessage());
		}
	}
	
	@UiThread
	void initData(List<DrawHistoryModel> data){
		list.clear();
		list.addAll(data);
		if(list!=null&&list.size()>0){
			adapter = new TiXianAdapter(this,list);
			listview_tixian.setAdapter(adapter);
		}else{
			text.setVisibility(View.VISIBLE);
		}
		pageNum++;
	}
	
	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}
	
	
	@Click
	void tixian_back() {
		finish();
	}
	
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 AVAnalytics.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}

}
